using System.Xml;
using UnityEngine;

public class GameRecorder : MonoBehaviour
{
	public Canvas currentCanvas;

	public bool isRecording;

	private int recordFrameCount;

	private Vector3 tempTouchStart = Vector3.zero;

	private int tempTouchStartFrame;

	private bool tempFlickBegan;

	private XmlDocument xmlDocument;

	private XmlNode rootNodeBallData;

	private XmlNode rootNodeTouchData;

	private string fileName;

	private string filePath;

	private void OnEnable()
	{
		Init();
	}

	private void OnDisable()
	{
		isRecording = false;
	}

	private void Init()
	{
		isRecording = false;
		filePath = Application.persistentDataPath + "/";
		fileName = "GameRecording_" + Game.currentLevelName;
		fileName = fileName.Replace(".", string.Empty) + ".xml";
	}

	private void FixedUpdate()
	{
		if (!isRecording)
		{
			return;
		}
		Vector3 recPosition;
		Quaternion recRotation;
		Vector3 recVelocity;
		Vector3 recAngularVelocity;
		bool recTouch;
		Vector3 recTouchPosition;
		Utils.TouchState recTouchState;
		BallBehaviour.GetInstance().GetRecordData(out recPosition, out recRotation, out recVelocity, out recAngularVelocity, out recTouch, out recTouchPosition, out recTouchState);
		if (recTouch)
		{
			switch (recTouchState)
			{
			case Utils.TouchState.touchBegin:
				tempFlickBegan = true;
				tempTouchStartFrame = recordFrameCount;
				tempTouchStart = recTouchPosition;
				break;
			case Utils.TouchState.touchEnd:
				if (tempFlickBegan)
				{
					tempFlickBegan = false;
					Vector3 vector = ConvertTouchPosToScreenRelative(tempTouchStart);
					Vector3 vector2 = ConvertTouchPosToScreenRelative(recTouchPosition);
					float number = Time.fixedDeltaTime * (float)(recordFrameCount - tempTouchStartFrame);
					XmlNode xmlNode = xmlDocument.CreateElement("t");
					CreateAttribute(xmlDocument, xmlNode, "f", tempTouchStartFrame.ToString());
					CreateAttribute(xmlDocument, xmlNode, "posS", Utils.Vector3ToString(vector, 5));
					CreateAttribute(xmlDocument, xmlNode, "posE", Utils.Vector3ToString(vector2, 5));
					CreateAttribute(xmlDocument, xmlNode, "dur", Utils.RoundFloat(number, 5).ToString("F5"));
					rootNodeTouchData.AppendChild(xmlNode);
				}
				break;
			}
		}
		XmlNode xmlNode2 = xmlDocument.CreateElement("b");
		CreateAttribute(xmlDocument, xmlNode2, "pos", Utils.Vector3ToString(recPosition, 5));
		CreateAttribute(xmlDocument, xmlNode2, "rot", Utils.QuaternionToString(recRotation, 5));
		CreateAttribute(xmlDocument, xmlNode2, "vel", Utils.Vector3ToString(recVelocity, 5));
		CreateAttribute(xmlDocument, xmlNode2, "avel", Utils.Vector3ToString(recAngularVelocity, 5));
		rootNodeBallData.AppendChild(xmlNode2);
		recordFrameCount++;
	}

	private Vector3 ConvertTouchPosToScreenRelative(Vector3 touchPos)
	{
		touchPos.x /= Screen.width;
		touchPos.y /= Screen.height;
		return touchPos;
	}

	public void StartRecording()
	{
		isRecording = true;
		recordFrameCount = 0;
		tempFlickBegan = false;
		if (xmlDocument != null)
		{
			xmlDocument = null;
		}
		xmlDocument = new XmlDocument();
		XmlNode xmlNode = xmlDocument.CreateElement("GameRecord");
		xmlDocument.AppendChild(xmlNode);
		rootNodeBallData = xmlDocument.CreateElement("BallData");
		xmlNode.AppendChild(rootNodeBallData);
		rootNodeTouchData = xmlDocument.CreateElement("TouchData");
		xmlNode.AppendChild(rootNodeTouchData);
	}

	public void StopRecording()
	{
		if (isRecording)
		{
			isRecording = false;
			xmlDocument.PreserveWhitespace = true;
			float realtimeSinceStartup = Time.realtimeSinceStartup;
			xmlDocument.Save(filePath + fileName);
			Debug.Log("GameRecorder -> StopRecording - saving xml took: " + (Time.realtimeSinceStartup - realtimeSinceStartup).ToString("f9") + " path:" + filePath + fileName);
			xmlDocument.RemoveAll();
			xmlDocument = null;
		}
	}

	private void CreateAttribute(XmlDocument xmlDoc, XmlNode parentXmlNode, string attributeName, string attributeValue)
	{
		XmlAttribute xmlAttribute = xmlDoc.CreateAttribute(attributeName);
		xmlAttribute.Value = attributeValue;
		parentXmlNode.Attributes.Append(xmlAttribute);
	}
}
